package com.ruoyi.carShopService.controller;


import com.ruoyi.carShopService.domain.ShopUserDto;
import com.ruoyi.carShopService.service.RegisterShopUserService;
import com.ruoyi.carShopService.util.SmsUtil;
import com.ruoyi.system.api.domain.CarMerchant;
import com.ruoyi.system.api.domain.CarMerchantPrincipal;
import com.ruoyi.system.api.domain.CarMerchantVerification;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/register")
@CrossOrigin
public class RegisterShopUserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RegisterShopUserService service;

    @RequestMapping("/update")
    public String mess(@RequestBody ShopUserDto shopUserDto) {
        Object phoneCode = redisTemplate.opsForValue().get("phoneCode");

        if (phoneCode != null && phoneCode.equals(shopUserDto.getPhoneNum())) {
            CarMerchant carMerchant = new CarMerchant();
            CarMerchantPrincipal principal = new CarMerchantPrincipal();
            CarMerchantVerification verification = new CarMerchantVerification();
            carMerchant.setId(shopUserDto.getId());
            principal.setMerId(shopUserDto.getId());
            verification.setMerId(shopUserDto.getId());
            carMerchant.setMerName(shopUserDto.getShopName());
            carMerchant.setMerArea(shopUserDto.getShopAddress());
            carMerchant.setImgPath(shopUserDto.getFormFileList3().stream().collect(Collectors.joining(",")));
            carMerchant.setMerAddress(shopUserDto.getShopHome());
            carMerchant.setMerIntroduction(shopUserDto.getMessage());
            principal.setPcpName(shopUserDto.getName());
            principal.setIdNumber(shopUserDto.getIdNum());
            principal.setPhoneNumber(shopUserDto.getPhone());
            principal.setIdCardFront(shopUserDto.getFormFileList1().get(0));
            principal.setIdCardBack(shopUserDto.getFormFileList1().get(1));
            verification.setMerLicenseNumber(shopUserDto.getShopId());
            verification.setMerLicenseImage(shopUserDto.getFormFileList2().stream().collect(Collectors.joining(",")));
            service.updateShopUser(carMerchant, principal, verification);
            redisTemplate.delete("phoneCode");
        }
        return "验证码错误";
    }

    @RequestMapping("/sendMessage")
    public String sendMessage(@RequestBody String phone) {
        String code = SmsUtil.createCode();
        redisTemplate.opsForValue().set("phoneCode", code, 300, TimeUnit.SECONDS);
        SmsUtil.send(phone, code);
        return "发送成功";
    }

}
